<?php
/**
 * [WeEngine System] Copyright (c) 2013 WE7.CC
 */
defined('IN_IA') or exit('Access Denied');
$do = !empty($_GPC['do']) ? $_GPC['do'] : 'display';
if (in_array($do, array('display', 'form', 'enable'))) {
	checklogin();
	if (empty($_W['weid']) || empty($_W['account'])) {
		message('请您从“管理公众号”或是从顶部“切换公众号”选择要操作的公众号！', '', 'error');
	}
}
$nav[$do] = ' class="current"';
if ($do == 'display') {
	$sql = "SELECT * FROM " . tablename('modules') . ' ORDER BY `mid` DESC';
    $modules = pdo_fetchall($sql, array(), 'mid');
    $mymodules = pdo_fetchall("SELECT mid, enabled, displayorder FROM ".tablename('wechats_modules')." WHERE weid = '{$_W['weid']}' ORDER BY enabled DESC, displayorder ASC, mid ASC", array(), 'mid');
    //拼接模块
    foreach ($mymodules as $mid => $row) {
    	if (!empty($modules[$mid])) {
    		$mymodules[$mid] = array_merge($mymodules[$mid], $modules[$mid]);
    		unset($modules[$mid]);
    	}
    }
    $mymodules = array_merge($mymodules, $modules);
    template('setting/module');	
} elseif ($do == 'enable') {
	$mid = intval($_GPC['mid']);
	$module = pdo_fetch("SELECT mid, issystem FROM ".tablename('modules')." WHERE mid = :mid", array(':mid' => $mid));
	if (empty($module)) {
		message('抱歉，模块不存在或是已经被删除！');
	}
	$exist = pdo_fetchcolumn("SELECT id FROM ".tablename('wechats_modules')." WHERE mid = :mid AND weid = :weid", array(':mid' => $mid, ':weid' => $_W['weid']));
	if (empty($exist)) {
		pdo_insert('wechats_modules', array(
			'mid' => $mid,
			'weid' => $_W['weid'],
			'enabled' => empty($_GPC['enabled']) ? 0 : 1,
			'displayorder' => $module['issystem'] ? '-1' : 127,
		));	
	} else {
		pdo_update('wechats_modules', array(
			'mid' => $mid,
			'weid' => $_W['weid'],
			'enabled' => empty($_GPC['enabled']) ? 0 : 1,
			'displayorder' => $module['issystem'] ? '-1' : 127,
		), array('id' => $exist));
	}
	cache_delete('setting:modules:'.$_W['weid']);
	message('模块操作成功！', '', 'success');
} elseif ($do == 'form') {
	include model('rule');
	if (empty($_GPC['name'])) {
		message('抱歉，模块不存在或是已经被删除！');
	}
	$modulename = !empty($_GPC['name']) ? $_GPC['name'] : 'basic';
	$module = module($modulename);
	if (is_error($module)) {
		exit($module['errormsg']);
	}
	$rid = intval($_GPC['id']);
	exit($module->fieldsFormDisplay($rid));
} elseif ($do == 'displayorder') {
	$mid = intval($_GPC['mid']);
	$displayorder = intval($_GPC['displayorder']);
	$module = pdo_fetch("SELECT mid, issystem FROM ".tablename('modules')." WHERE mid = :mid", array(':mid' => $mid));
	if (empty($module)) {
		message('抱歉，模块不存在或是已经被删除！');
	}
	if ($module['issystem']) {
		message('抱歉，系统模块无法设置优先级！');
	}
	pdo_query("UPDATE ".tablename('wechats_modules')." SET displayorder = 127 WHERE displayorder = '$displayorder' AND weid = '{$_W['weid']}'");
	pdo_update('wechats_modules', array('displayorder' => $displayorder == 0 ? 127 : $displayorder), array('mid' => $mid ,'weid' => $_W['weid']));
	cache_delete('setting:modules:'.$_W['weid']);
	message('操作成功！', referer());
} else {
	include model('rule');	
	if (empty($_GPC['name'])) {
		message('抱歉，模块不存在或是已经被！');
	}
	$modulename = !empty($_GPC['name']) ? $_GPC['name'] : 'basic';
	$module = module($modulename);
	if (is_error($module)) {
		exit($module['errormsg']);
	}
	
	$method = 'do'.$_GPC['do'];
	if (method_exists($module, $method)) {
		exit($module->$method());
	}
}